package com.bearsadmin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bearsadmin.common.Result;
import com.bearsadmin.dto.UserCreateDTO;
import com.bearsadmin.dto.UserManageDTO;
import com.bearsadmin.dto.UserQueryDTO;
import com.bearsadmin.service.UserService;
import com.bearsadmin.vo.UserVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理控制器
 *
 * @author Bears Admin
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/manage/user")
@RequiredArgsConstructor
public class UserManageController {

    private final UserService userService;

    /**
     * 分页查询用户列表
     *
     * @param queryDTO 查询条件
     * @return 分页用户列表
     */
    @GetMapping("/page")
    public Result<IPage<UserVO>> pageUsers(UserQueryDTO queryDTO) {
        try {
            IPage<UserVO> userPage = userService.pageUsers(queryDTO);
            return Result.success(userPage);
        } catch (Exception e) {
            log.error("分页查询用户失败", e);
            return Result.error("查询用户列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取用户信息
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public Result<UserVO> getUserById(@PathVariable Long id) {
        try {
            UserVO userVO = userService.getUserById(id);
            return Result.success(userVO);
        } catch (Exception e) {
            log.error("获取用户信息失败: {}", id, e);
            return Result.error("获取用户信息失败: " + e.getMessage());
        }
    }

    /**
     * 创建用户
     *
     * @param createDTO 用户创建信息
     * @return 创建的用户信息
     */
    @PostMapping
    public Result<UserVO> createUser(@Validated @RequestBody UserCreateDTO createDTO) {
        try {
            UserVO userVO = userService.createUser(createDTO);
            return Result.success(userVO);
        } catch (Exception e) {
            log.error("创建用户失败", e);
            return Result.error("创建用户失败: " + e.getMessage());
        }
    }

    /**
     * 更新用户信息
     *
     * @param id        用户ID
     * @param manageDTO 更新信息
     * @return 更新后的用户信息
     */
    @PutMapping("/{id}")
    public Result<UserVO> updateUser(@PathVariable Long id,
            @Validated @RequestBody UserManageDTO manageDTO) {
        try {
            UserVO userVO = userService.manageUser(id, manageDTO);
            return Result.success(userVO);
        } catch (Exception e) {
            log.error("更新用户信息失败: {}", id, e);
            return Result.error("更新用户信息失败: " + e.getMessage());
        }
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@PathVariable Long id) {
        try {
            boolean success = userService.deleteUser(id);
            if (success) {
                return Result.success(null);
            } else {
                return Result.error("删除用户失败");
            }
        } catch (Exception e) {
            log.error("删除用户失败: {}", id, e);
            return Result.error("删除用户失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除用户
     *
     * @param ids 用户ID列表
     * @return 删除结果
     */
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteUsers(@RequestBody List<Long> ids) {
        try {
            boolean success = userService.batchDeleteUsers(ids);
            if (success) {
                return Result.success(null);
            } else {
                return Result.error("批量删除用户失败");
            }
        } catch (Exception e) {
            log.error("批量删除用户失败: {}", ids, e);
            return Result.error("批量删除用户失败: " + e.getMessage());
        }
    }

    /**
     * 重置用户密码
     *
     * @param id 用户ID
     * @return 新密码
     */
    @PostMapping("/{id}/reset-password")
    public Result<String> resetPassword(@PathVariable Long id) {
        try {
            String newPassword = userService.resetPassword(id);
            return Result.success(newPassword);
        } catch (Exception e) {
            log.error("重置用户密码失败: {}", id, e);
            return Result.error("重置密码失败: " + e.getMessage());
        }
    }
}